package com.daniel.juc.volat;

/**
 * 详细说明
 *
 * @author Daniel
 * @version V1.0
 * @date 2020/11/4 14:47
 */
public class Tester {

    volatile static boolean flag;

    public static void main(String[] args) {
        new Thread1().start();
        new Thread2().start();
    }

    static class Thread1 extends Thread {
        @Override
        public void run() {
            while (true) {
                if (flag) {
                    flag = false;
                    System.out.println("Thread1 set flag to false.");
                }
            }
        }
    }

    static class Thread2 extends Thread {
        @Override
        public void run() {
            while (true) {
                if (!flag) {
                    flag = true;
                    System.out.println("Thread2 set flag to true.");
                }
            }
        }
    }
}
